package com.app.controller;

import com.app.model.AdminUserModel;
import com.app.model.CarModel;
import com.app.model.CarPrivateModel;
import com.app.model.CategoryModel;
import com.app.service.AdminUserService;
import com.app.service.CarPrivateService;
import com.app.service.CarService;
import com.app.service.CategoryService;
import com.app.util.CommonHelp;
import com.app.util.Result;
import com.app.util.TokenUtils;
import com.app.vo.CarPrivateVo;
import com.app.vo.CarVo;
import com.app.vo.CategoryVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 车辆信息管理
 */
@RestController
@Controller
@RequestMapping("/api/carPrivate")
public class CarPrivateController {
    @Autowired
    private CarPrivateService carPrivateService;
    @Autowired
    private AdminUserService adminUserService;

    //新增和编辑
    @PostMapping("/insertCar")
    public Result<Object> insertCar(@RequestBody CarPrivateModel carPrivateModel, HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        AdminUserModel user = adminUserService.getUserByUserId(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        if (carPrivateModel.getId() > 0) {
            carPrivateModel.setUpdateTime(CommonHelp.getNowTimestamp());
            carPrivateService.update(carPrivateModel);
        } else {
            carPrivateModel.setCreateTime(CommonHelp.getNowTimestamp());
            carPrivateService.insert(carPrivateModel);
        }
        return Result.success("操作成功");
    }

    //删除
    @GetMapping("/deleteCar")
    public Result<Object> deleteCar(Integer id, HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        AdminUserModel user = adminUserService.getUserByUserId(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        carPrivateService.delete(id);
        return Result.success("操作成功");
    }

    //车辆信息批量删除
    @DeleteMapping("/deleteCarBatchByIds")
    public Result<Object> deleteCarBatchByIds(String ids,HttpServletRequest request){
        // 校验token并获取用户信息
        AdminUserModel user = validateTokenAndGetUser(request);
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        return carPrivateService.deleteCarBatchByIds(ids);
    }
    // 校验token并获取用户信息的方法
    private AdminUserModel validateTokenAndGetUser(HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            throw new IllegalArgumentException("token不存在");
        }
        String userId = TokenUtils.verify(token);
        return adminUserService.getUserByUserId(Integer.parseInt(userId));
    }



    //查询列表
    @GetMapping("/selectCar")
    public Result<Object> selectCar(Integer page, Integer limit, String carNumber, Integer departmentId,HttpServletRequest request,Integer sortType) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        AdminUserModel user = adminUserService.getUserByUserId(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        CarPrivateVo vo = new CarPrivateVo();
        Page<CarPrivateModel> taskModelPage = carPrivateService.selectCarList(page, limit, carNumber,departmentId,sortType);
        List<CarPrivateModel> records = taskModelPage.getRecords();
        int total = (int) taskModelPage.getTotal();
        vo.setList(records);
        vo.setCount(total);
        return Result.success(vo);
    }
}
